package cn.ms.doctor.Service;

import cn.ms.doctor.Entity.Order;
import cn.ms.doctor.Mapper.DrugsMapper;
import cn.ms.doctor.Mapper.DrugsorderMapper;
import cn.ms.doctor.Mapper.OrderMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    OrderMapper orderMapper;

    @Resource
    DrugsorderMapper drugsorderMapper;

    @Resource
    DrugsMapper drugsMapper;

    @Override
    public Map<String, Object> getOrders(Map<String, Object> params) {
        Map<String, Object> data=new HashMap<>();
        Page<Object> objects = PageHelper.startPage((Integer) params.get("pageIndex"), (Integer) params.get("pageSize"));
        List<Map<String,Object>> orderDatas=orderMapper.getOrderBySearch(params);
        for (Map<String, Object> orderData : orderDatas) {
            orderData.put("drugs",drugsMapper.getDrugsByDrugIds(drugsorderMapper.getDrugsIdByOrderId((Integer) orderData.get("orderId"))));
        }
        data.put("pageData",orderDatas);
        data.put("pageIndex",objects.getPageNum());
        data.put("pageSize",objects.getPageSize());
        data.put("pageCount",objects.getPages());
        return data;
    }

    @Override
    public boolean updateStateByOrderId(Order order) {
        return orderMapper.updateStateByOrderId(order);
    }
}
